<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input 
        v-model="listQuery.filters.nickname" 
        placeholder="理财师姓名" 
        style="width: 200px;" 
        class="filter-item"/>
      <el-input 
        v-model="listQuery.filters.mobile" 
        placeholder="理财师手机号" 
        style="width: 200px;" 
        class="filter-item"/>
      <el-button 
        class="filter-item" 
        type="primary" 
        icon="el-icon-search" 
        @click="handleFilter">{{ $t('all.search') }}</el-button>
    </div>
    <el-table
      v-loading="listLoading"
      :key="tableKey"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
    >
      <el-table-column 
        :label="$t('user.nickname')" 
        align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.nickname }}</span>
        </template>
      </el-table-column>

      <el-table-column 
        :label="$t('user.mobile')" 
        align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.mobile }}</span>
        </template>
      </el-table-column>

      <el-table-column 
        :label="$t('user.userType')" 
        align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.userType|userType }}</span>
        </template>
      </el-table-column>

      <el-table-column 
        label="是否内部人员" 
        align="center">
        <template slot-scope="scope">
          <el-button 
            v-if="scope.row.type==2" 
            size="mini" 
            type="success" 
            @click="handleModifyStatus(scope.row,1)">是</el-button>
          <el-button 
            v-if="scope.row.type==1" 
            size="mini" 
            type="info" 
            @click="handleModifyStatus(scope.row,2)">否</el-button>
        </template>
      </el-table-column>

      <el-table-column 
        :label="$t('all.actions')" 
        align="center" 
        class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button 
            type="primary" 
            size="mini" 
            @click="handleUpdate(scope.row.id)">详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <user-detail ref="tempForm"/>
    <pagination 
      v-show="total>0" 
      :total="total" 
      :page.sync="listQuery.page" 
      :limit.sync="listQuery.pageSize" 
      @pagination="getList" />

  </div>
</template>

<script>
import { fetchList, fetch,updateUserType } from '@/api/user'
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination
import UserDetail from './form'

const userType = {
  1:'金牌理财师',
  2:'银牌理财师',
};
export default {
  name: 'UserIndex',
  components: { Pagination, UserDetail },
  filters:{
    userType:function (status) {
      return userType[status];
    }
  },
  data() {
    return {
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        pageSize: 20,
        order: 'desc',
        sort: 'id',
        filters: {
          mobile: '', //
          nickname: '',
        }
      },
      downloadLoading: false
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      fetchList(this.listQuery).then(response => {
        this.list = response.data.rows
        this.total = response.data.total

        // Just to simulate the time of the request
        setTimeout(() => {
          this.listLoading = false
        }, 0.5 * 1000)
      })
    },
    handleModifyStatus(row, status) {
      updateUserType(row).then((res) => {
        if (res.data.code == 1) {
          this.$message({
            message: '操作成功',
            type: 'success'
          })
          row.type = status
        } else {
          this.$message({
            showClose: true,
            message: '更新失败',
            type: 'error'
          })
        }
      }).catch((err) => {
        this.$message({
          message: '网络请求失败',
          type: 'error'
        })
      })
    },
    handleFilter() {
      this.getList()
    },
    handleUpdate(row) {
      this.$refs.tempForm.formShow(row, '', 3)
    }
  }
}
</script>

<style scoped>
  .avatar{
    width: 200px;
    height: 200px;
    border-radius: 50%;
  }
</style>
